package util;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
//使排行榜表格背景为透明
public class TransparentTable {
    public static void setTransparentTable(JTable table) {

        table.setOpaque(false); // 使整个表格背景透明

        // 获取表格的默认渲染器
        TableCellRenderer defaultRenderer = table.getDefaultRenderer(Object.class);

        // 创建自定义渲染器，将背景色设置为透明，并将文本居中对齐
        TableCellRenderer transparentRenderer = new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                component.setBackground(new Color(0, 0, 0, 0)); // 使用透明色
                return component;
            }
        };

        // 将自定义渲染器设置为表格的默认渲染器
        table.setDefaultRenderer(Object.class, transparentRenderer);
    }
}